package com.quanyue.haircut.order.controller;

import com.quanyue.auto.util.ServiceResult;
import com.quanyue.auto.web.RequestContext;
import com.quanyue.auto.web.WebUtils;
import com.quanyue.haircut.domain.entity.CardOrder;
import com.quanyue.haircut.domain.entity.User;
import com.quanyue.haircut.order.dto.CardOrderResult;
import com.quanyue.haircut.order.service.CardOrderService;
import com.quanyue.haircut.user.mapper.UserMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;

@Api(tags = "会员卡充值")
@RestController
@RequestMapping("/api")
public class CardOrderController {
    @Autowired
    private CardOrderService cardService;
    @Autowired
    private UserMapper userMapper;

    @ApiOperation(value = "创建订单")
    @PostMapping(value = "/card/order/create", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <CardOrderResult> createOrder(@RequestBody CardOrder cardOrder,
                                                       @ApiIgnore HttpServletRequest request,
                                                       @ApiIgnore RequestContext context) {
        User user = context.getUser();
        if (user == null) user = userMapper.findOne(cardOrder.getUserId());
        return ServiceResult.success(cardService.create(user.getMerchantNo(), user, cardOrder, WebUtils.getClientIp(request)));
    }

}
